package com.tnovoselec.android.travelassistant.adapter;

import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.nth.android.image.ImageDownloader;
import com.tnovoselec.android.travelassistant.R;
import com.tnovoselec.android.travelassistant.Util;
import com.tnovoselec.android.travelassistant.dto.CurrencyDto;

public class ExchangeRatesAdapter extends BaseAdapter {

	private List<CurrencyDto> rates;
	private LayoutInflater inflater;
	private Context mContext;
	private CurrencyDto tempCurrency;

	public ExchangeRatesAdapter(Context c) {
		this.mContext = c;
		inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		return rates != null ? rates.size() : 0;
	}

	@Override
	public Object getItem(int position) {
		return rates.get(position);
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View v;
		ViewHolder holder;
		tempCurrency = rates.get(position);
		if (convertView == null) {
			holder = new ViewHolder();
			v = inflater.inflate(R.layout.exchange_rates_item, parent, false);
			holder.image = (ImageView) v.findViewById(R.id.exchange_rates_item_image);
			holder.text = (TextView) v.findViewById(R.id.exchange_rates_item_text);
			holder.name = (TextView)v.findViewById(R.id.exchange_rates_item_name);
			v.setTag(holder);
		} else {
			v = convertView;
		}
		holder = (ViewHolder) v.getTag();
	//	ImageDownloader.getInstance().download(tempCurrency.getImageFlagUrl(), holder.image, true);
		
		holder.image.setImageResource(mContext.getResources().getIdentifier(tempCurrency.getName().toLowerCase(), "drawable", mContext.getPackageName()));
		holder.text.setText(tempCurrency.getUnit() + " " + tempCurrency.getName() + " : " + tempCurrency.getMeanRate() + " HRK");
		holder.name.setText(Util.currencyNames.get(tempCurrency.getName()));
		return v;
	}

	class ViewHolder {
		ImageView image;
		TextView text;
		TextView name;
	}
	
	public void setData(List<CurrencyDto> items){
		this.rates = items;
		notifyDataSetChanged();
	}
}
